package com.shen.lock;

import java.util.concurrent.TimeUnit;

public class DeadLockDemo {
    public static void main(String[] args) {
        String lockA="lockA";
        String lockB="lockB";
        new Thread(new MyThread(lockA,lockB),"T1").start();
        new Thread(new MyThread(lockB,lockA),"T2").start();

        /**
         * T1lock:lockA get==>lockB     线程T1获得了lockA的锁但它还想获得lockB的锁
         * T2lock:lockB get==>lockA     线程T2获得了lockB的锁但它想要获得lockA的锁
         */
    }

}

class MyThread implements Runnable{
    private String  lockA;
    private String lockB;

    public MyThread(String lockA, String lockB) {
        this.lockA = lockA;
        this.lockB = lockB;
    }

    @Override
    public void run() {
        /**
         * 制造死锁
         */
        synchronized (lockA){
            System.out.println(Thread.currentThread().getName()+"lock:"+lockA+"get==>"+lockB);
            try {
                TimeUnit.SECONDS.sleep(2);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            synchronized (lockB){
                System.out.println(Thread.currentThread().getName()+"lock:"+lockB+"get===>"+lockA);
            }
        }
    }
}
